package files

import (
	"io/fs"
	"os"
	"path"
	"path/filepath"
)

func WalkPathChild(dirPath string, skips ...func(fullpath string, dirEntry os.DirEntry) bool) ([]string, error) {
	if _, err := os.Stat(path.Dir(dirPath)); err != nil && os.IsNotExist(err) {
		return nil, err
	}
	mediaPath := make([]string, 0)
	err := filepath.WalkDir(path.Dir(dirPath), func(path string, d fs.DirEntry, err error) error {
		for _, handle := range skips {
			if handle(path, d) {
				return nil
			}
		}
		mediaPath = append(mediaPath, path)
		return nil
	})
	if err != nil {
		return nil, err
	}
	return mediaPath, nil
}
func WalkCurrentDirPath(dirPath string, skips ...func(fullpath string, dirEntry os.DirEntry) bool) ([]string, error) {
	dir := path.Dir(dirPath)
	if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
		return nil, err
	}
	mediaPath := make([]string, 0)
	dirInfo, err := os.ReadDir(dirPath)
	if err != nil {
		return nil, err
	}
	for _, entry := range dirInfo {
		for _, skip := range skips {
			join := filepath.Join(dir, entry.Name())
			if !skip(join, entry) {
				mediaPath = append(mediaPath, join)
				break
			}
		}
	}
	return mediaPath, nil
}
